﻿using Dln.Common;
using Dln.Common.Helper;
using Dln.Handler;
using Dln.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Page_Customer_FoodListDetial : BasePage
{
    public string openid = String.Empty;
    public PatientInfo Model = null;
    public Dln.Common.ServiceReference1.OrderDinerModel[] orderDinerModel = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        //openid = "oF6dUwxAa6BtWPqaRnE_lz7lDBgQ";
        openid = GetAuthOpenId();
        Model = SqlHelper.Context.From<PatientInfo>().Where(d => d.Openid == openid && d.Delstatus == 0).First();
        List<OrderDinerModel> a = new List<OrderDinerModel>();
        OrderDinner();

    }


    public void OrderDinner()
    {
        if (Model != null && !string.IsNullOrEmpty(Model.PatientPatiID))
        {
            Dln.Common.ServiceReference1.Service1SoapClient client2 = new Dln.Common.ServiceReference1.Service1SoapClient();
            orderDinerModel = client2.GetOrderDinner(Model.PatientPatiID, DateTime.Now.ToShortDateString());
        }
        
    }

    public class FoodModel
    {
        public string MeanID { get; set; }
        public string FoodDate { get; set; }
        public string FoodPart { get; set; }
        public string FoodName { get; set; }
    }

    public class OrderDinerModel
    {
        public string FoodDate { get; set; }
        public string FoodPart { get; set; }
        public string Serilanumber { get; set; }
        public List<FoodModel> FoodList = new List<FoodModel>();
    }

    public string GetAuthOpenId()
    {
        string AppId = CommonHelper.GetAppSettings("AppID");
        string AppSecret = CommonHelper.GetAppSettings("AppSecret");
        string strOpenId = "";
        string name = "";
        string code = DNTRequest.GetString("CODE");

        var cookies = HttpContext.Current.Request.Cookies["openidcookie"];
        if (cookies != null && !string.IsNullOrEmpty(cookies.Value))
        {
            strOpenId = cookies.Value;
        }
        else
        {
            if (!String.IsNullOrEmpty(code))
            {
                string json = RequestHelper.SendGet(String.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppId, AppSecret, code));
                if (!String.IsNullOrEmpty(json))
                {
                    try
                    {
                        OAuthConfig oAuthConfig = JSONHelper.FromJson<OAuthConfig>(json);
                        if (oAuthConfig != null)
                        {
                            HttpCookie cookie = new HttpCookie("openidcookie");
                            cookie.Value = oAuthConfig.openid;
                            cookie.Expires = DateTime.Now.AddDays(2);
                            HttpContext.Current.Response.Cookies.Add(cookie);
                            strOpenId = oAuthConfig.openid;
                        }

                        else
                        {
                            strOpenId = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        strOpenId = "";
                    }
                }
            }
            else
            {
                var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}%2FPage%2FCustomer%2FFoodListDetial&response_type=code&scope=snsapi_base&state=test#wechat_redirect", AppId, CommonHelper.GetAppSettings("mainurl").Replace(@"://", "%3A%2F%2F"));
                Response.Redirect(url);

            }
        }
        return strOpenId;
    }
}